package com.situ.stl.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.situ.stl.constants.Constants;
import com.situ.stl.entity.Coach;
import com.situ.stl.entity.Course;
import com.situ.stl.service.CoachService;
import com.situ.stl.service.CourseService;
import com.situ.stl.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class FileController {

    @Autowired
    private CoachService coachService;



    @Value("${alinfo.oss-bucketname}")
    private String bucketName;
    @Value("${alinfo.oss-endpoint}")
    private String endpoint;
    @Value("${alinfo.oss-accesskeyid}")
    private String accessKeyId;
    @Value("${alinfo.oss-accesskeysecret}")
    private String accessKeySecret;


    @RequestMapping("/savePic")
    public Result savePic(@RequestParam("file") MultipartFile file) {
        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
        String filename = file.getOriginalFilename(); // 4.5.6.7.82.jpg
        String uuidStr = UUID.randomUUID().toString().replace("-",""); // uuid字符串
        System.out.println(uuidStr);
        filename = filename.substring(filename.lastIndexOf("."));
        String newFileName = uuidStr + filename;
        String userName = "guozedong";
        String objectName = userName+"/"+newFileName;
        // 创建OSS对象
        OSS ossClient = null;
        try {
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            //上传
            ossClient.putObject(bucketName, objectName, file.getInputStream());
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            ossClient.shutdown();
        }

        String path = "https://zxst-shoop-122.oss-cn-qingdao.aliyuncs.com/" + objectName;



            return Result.success(Constants.STATUS_200,path);



    }
    @RequestMapping("/savePic1")
    public Result savePic1(@RequestParam("file") MultipartFile file) {
        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
        String filename = file.getOriginalFilename(); // 4.5.6.7.82.jpg
        String uuidStr = UUID.randomUUID().toString().replace("-",""); // uuid字符串
        System.out.println(uuidStr);
        filename = filename.substring(filename.lastIndexOf("."));
        String newFileName = uuidStr + filename;
        String userName = "guozedong1";
        String objectName = userName+"/"+newFileName;
        // 创建OSS对象
        OSS ossClient = null;
        try {
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            //上传
            ossClient.putObject(bucketName, objectName, file.getInputStream());
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            ossClient.shutdown();
        }

        String path = "https://zxst-shoop-122.oss-cn-qingdao.aliyuncs.com/" + objectName;



        return Result.success(Constants.STATUS_200,path);



    }
}
